package com.controller;

import com.beans.ErrorStatusEnum;
import com.beans.FileDepotEntity;
import com.beans.RadioDepotEntity;
import com.common.entity.R;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wyj
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("radioDepot")
public class RadioDepotController {

    @PersistenceContext
    private EntityManager entityManager;

    @PostMapping("mergeRadioDepot")
    public R mergeRadioDepot(HttpServletRequest request, RadioDepotEntity radioDepotEntity) {
        radioDepotEntity.setCreateTime(LocalDateTime.now());
        radioDepotEntity.setState(1);

        radioDepotEntity.setAuthor(request.getSession().getAttribute("username")+"");
        entityManager.merge(radioDepotEntity);
        return R.ok();
    }

    /**
     * 分页获取
     *
     * @param state 状态(0.停用 1.正常 )
     * @param page  第几页
     * @param rows  每页几条数据
     * @param sort  排序字段名
     * @param order 排序方式  asc desc
     * @return com.common.entity.R
     * @author gm
     * @date 2020/6/5
     */
    @GetMapping("radioDepotPage")
    public R radioDepotPage(Integer state, Integer page, Integer rows, String sort, String order) {
        //region 组装hql
        StringBuilder sql = new StringBuilder("select * from radio_depot where 1 = 1 ");
        StringBuilder countSql = new StringBuilder("select count(id) from radio_depot where 1 = 1 ");
        if (state != null) {
            sql.append(" and state = :state ");
            countSql.append(" and state = :state ");
        }
        if (!StringUtils.isEmpty(sort)) {
            sql.append(" order by ").append(sort).append(" ");
            if (!StringUtils.isEmpty(order)) {
                sql.append(order);
            }
        }
        if (page != null && rows != null) {
            sql.append(" limit :offset , :rows ");
        }
        //endregion 组装hql
        //region 组装查询对象和参数
        Query query = entityManager.createNativeQuery(sql.toString(), RadioDepotEntity.class);
        Query queryCount = entityManager.createNativeQuery(countSql.toString());
        if (state != null) {
            query.setParameter("state", state);
            queryCount.setParameter("state", state);
        }
        if (page != null && rows != null) {
            query.setParameter("offset", (page - 1) * rows).setParameter("rows", rows);
        }
        List<RadioDepotEntity> resultList = query.getResultList();
        Object count = queryCount.getSingleResult();
        R ok = R.ok();
        ok.put("total", count);
        ok.put("rows", resultList);
        return ok;
    }

    /**
     * 删除
     *
     * @param id
     * @return com.common.entity.R
     * @author gm
     * @date 2020/6/2
     */
    @PostMapping("deleteRadioDepot")
    public R deleteRadioDepot(Integer id) {
        if (id == null) {
            return R.error(ErrorStatusEnum.PARAMS_CANNOT_BE_NULL_ERROR.code(), "停用操作，id不能为空!");
        }
        entityManager.createNativeQuery("update radio_depot set state = 0 where id = :id").setParameter("id", id).executeUpdate();
        return R.ok();
    }


    @GetMapping("queryALL")
    public String queryALL() {
        List<RadioDepotEntity> radioList = entityManager.createNativeQuery("select * from radio_depot where  state = 1 ",RadioDepotEntity.class).getResultList();
        if (radioList.size() > 0) {
            new XmlDo4j().createRadioDocument(radioList);
        }


        List<FileDepotEntity> fileList = entityManager.createNativeQuery("select * from file_depot where  state = 1 ",FileDepotEntity.class).getResultList();
        if (radioList.size() > 0) {
            new XmlDo4j().createFileDocument(fileList);
        }


        return "";
    }



}
